﻿#include "maindrawwidget.h"

MainDrawWidget::MainDrawWidget(QWidget *parent) : QWidget(parent)
{
    pCurvesList = new QVector<PDATA_CONTEXT> ;
    pauseflag = false;
}


MainDrawWidget::~MainDrawWidget()
{
    delete pCurvesList;
}

// 设置刻度
void MainDrawWidget::setScale(int scale)
{
    this->scale = scale;

    return;
}


// 设置绘图列表
void MainDrawWidget::setCurvesList(QVector<PDATA_CONTEXT>* pCurvesList)
{
    this->pCurvesList = pCurvesList;
//    for(QVector<PDATA_CONTEXT>::iterator it = pCurvesList->begin(); it != pCurvesList->end(); ++it)
//    {
//        qDebug()<< *(*it)->title;
//    }

    return;

}

// 设置当前周期
void MainDrawWidget::setCurrentCycle(int cur_cycle)
{
    this->cur_cycle = cur_cycle;

    return;
}



void MainDrawWidget::setDataSource(int source)
{
    this->datasource = source;

    return;
}


void MainDrawWidget::setPause(bool flag)
{
    pauseflag = flag;

    return;
}


void MainDrawWidget::setSize(int width, int height)
{
    this->width = width;
    this->height = height;
    this->setFixedSize(width,height);

    return;
}


void MainDrawWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);                                        // 设置抗锯齿
    QPen pen;

    float ORIGIN_X = (float)width / 2;                                              // 坐标X原点
    float ORIGIN_Y = (float)height;                                                 // 坐标Y原点


    // 画其他刻度线
    pen.setStyle(Qt::DashLine);                                                 // 线条改虚线
    pen.setColor(NORMAL_CYCLE_COLOR);
    pen.setWidth(LINE_WIDTH);
    p.setPen(pen);

    for(int i = 1; i<MINOR_SCALE_NUMBERS; ++i)
    {
        if(i == (MINOR_SCALE_NUMBERS + 1) / 2)                                  // 画0刻度（当前周期）
        {
            pen.setColor(CURRENT_CYCLE_COLOR);
            p.setPen(pen);
            p.drawLine(i * ((float)width / (float)MINOR_SCALE_NUMBERS), 0, i * ((float)width / (float)MINOR_SCALE_NUMBERS), (float)height);
            pen.setColor(NORMAL_CYCLE_COLOR);
            p.setPen(pen);
            continue;
        }
        if( i % 5 !=0)                                                     // 画大刻度线
        {
            pen.setWidth(1);
            p.setPen(pen);
            p.drawLine(i * ((float)width / MINOR_SCALE_NUMBERS), 0, i * ((float)width / MINOR_SCALE_NUMBERS), height);
            pen.setWidth(LINE_WIDTH);
            p.setPen(pen);
            continue;
        }

        p.drawLine(i * ((float)width / MINOR_SCALE_NUMBERS), 0, i * ((float)width / MINOR_SCALE_NUMBERS), height);
        continue;
    }


    pen.setStyle(Qt::SolidLine);                                                // 线条改为实线
    p.setPen(pen);

    if(scale <= 0)                                                              //  判断刻度尺
    {
        return;
    }

    if(NULL == pCurvesList) return;


    // 计算绘图刻度
    float draw_scale = (float)width / (float)MINOR_SCALE_NUMBERS / (float)scale;

    //绘制所有曲线
    for (auto &item : std::as_const(*pCurvesList)) {
        int now_pos = 0;

        // 如果没有值或者显示标志位为false，则不绘制曲线
        if(!item->allCyclesIntValues || !*item->show_flag)
        {
            continue;
        }

        // 计算绘图用的坐标原点
        float origin_y =(ORIGIN_Y / (float)qAbs(*item->max - *item->min))*(float)qAbs(*item->max) - SPACE_LINE_TO_EDGE;

        // 设置画笔颜色为曲线颜色
        QPoint* draw_point_start = NULL;
        QPoint* draw_point_end = NULL;

        pen.setColor(QColor(*item->red, *item->green, *item->blue));
        p.setPen(pen);

        // 计算该曲线绘图的Y轴范围
        int show_range_max = (((float)MINOR_SCALE_NUMBERS / (float)MAJOR_SCALE_NUMBERS)*10) * (float)scale / 2;
        int show_range_min = -(((float)MINOR_SCALE_NUMBERS / (float)MAJOR_SCALE_NUMBERS)*10) * (float)scale / 2;

        //开始绘制曲线
        for(int i = 0; i < item->allCyclesIntValues->size(); ++i)
        {
            ++now_pos;
            float x;
            if(datasource == DATA_SOURCE::UDP)    // 计算实时获取的当前点的横坐标
            {
                if(pauseflag)
                {
                    if(now_pos - cur_cycle > show_range_max || now_pos - cur_cycle < show_range_min)
                    {
                        continue;
                    }
                    x = ORIGIN_X + (now_pos-cur_cycle) * draw_scale;
                }
                else
                {
                    if(now_pos - item->allCyclesIntValues->size() > show_range_max || now_pos - item->allCyclesIntValues->size() < show_range_min)
                    {
                        continue;
                    }
                    x = ORIGIN_X + (now_pos - item->allCyclesIntValues->size()) * draw_scale;
                }
            }
            else        // 计算从日志读取的当前点的横坐标
            {
                if(now_pos - cur_cycle > show_range_max || now_pos - cur_cycle < show_range_min)
                {
                    continue;
                }
                x = ORIGIN_X + (now_pos-cur_cycle) * draw_scale;
            }

            // 计算当前点的纵坐标
            float y= origin_y - (float)(*item->allCyclesIntValues->at(i)) * ((float)(origin_y - SPACE_LINE_TO_EDGE) / qAbs(*item->max - *item->min));
            // qDebug()<< "Value" << *item->allCyclesIntValues->at(i) << "纵坐标" << y;

            if(!draw_point_start)
            {
                draw_point_start = new QPoint(x, y);
            }

            if(!draw_point_end)
            {
                draw_point_end = new QPoint(x, y);
                continue;
            }

            if(datasource == DATA_SOURCE::LOGFILE || pauseflag)
            {
                // 画同高度的线
                if(y == draw_point_end->y())                               // 如果前后2点的纵坐标相等，则直接连接前后2点的线段
                {
                    if(now_pos - cur_cycle == show_range_max || now_pos - cur_cycle == show_range_min || now_pos == item->allCyclesIntValues->size())
                    {
                        p.drawLine(*draw_point_start, QPoint(x,y));
                        delete draw_point_start;
                        draw_point_start = new QPoint(x, y);
                        delete draw_point_end;
                        draw_point_end = new QPoint(x, y);
                        continue;
                    }
                    delete draw_point_end;
                    draw_point_end = new QPoint(x, y);
                    continue;
                }

            }


            float mid_x = x;                                     // 中间过度点的X坐标为当前点的X值
            float mid_y = draw_point_end->y();                        // 中间过度点Y轴坐标为前一个点的Y值
            p.drawLine(*draw_point_start, QPoint(mid_x, mid_y));      // 连接起点和中间点
            p.drawLine(QPoint(mid_x, mid_y), QPoint(x,y));    // 连接中间点和当前点
            delete draw_point_start;
            draw_point_start = new QPoint(x, y);
            delete draw_point_end;
            draw_point_end = new QPoint(x, y);
            continue;


        }
    }



    return;
}

